package com.vhall.component.service.rbac.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.rbac.RbacGroupAccessMapper;
import com.vhall.component.dao.rbac.RbacGroupMapper;
import com.vhall.component.entity.rbac.GroupEntity;
import com.vhall.component.entity.rbac.vo.*;
import com.vhall.component.service.rbac.RbacGroupService;
import com.vhall.component.service.rbac.constant.AccessConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dalong
 * @date 2021-05-10 18:08
 */
@Service
public class RbacGroupServiceImpl implements RbacGroupService {

    @Autowired
    private RbacGroupMapper groupMapper;

    @Autowired
    private RbacGroupAccessMapper groupAccessJoinMapper;


    @Override
    public GroupVO getInfoByGroupIdStatus(Integer groupId, Integer status) {
        LambdaQueryWrapper<GroupEntity> where = Wrappers.lambdaQuery();
        where.eq(GroupEntity::getId, groupId)
                .eq(GroupEntity::getStatus, status)
                .isNull(GroupEntity::getDeletedAt)
                .last("LIMIT 1");
        GroupEntity groupEntity = groupMapper.selectOne(where);
        GroupVO groupVO = new GroupVO();
        BeanUtils.copyProperties(groupEntity, groupVO);
        return groupVO;
    }

    @Override
    public GroupVO create(GroupCreateReqVO reqVO) {

        GroupEntity groupEntity = new GroupEntity();
        groupEntity.setGroupName(reqVO.getGroupName());
        groupEntity.setAppId(reqVO.getAppId());
        groupEntity.setStatus(AccessConstant.NORMAL_STATUS);
        groupEntity.setCreatedAt(LocalDateTime.now());
        groupEntity.setUpdatedAt(LocalDateTime.now());
        groupMapper.insert(groupEntity);
        groupEntity.setId(groupEntity.getId());

        GroupVO groupVO = new GroupVO();
        BeanUtils.copyProperties(groupEntity, groupVO);
        return groupVO;
    }

    @Override
    public boolean update(GroupUpdateReqVO reqVO) {
        // token check
        LambdaUpdateWrapper<GroupEntity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(GroupEntity::getId, reqVO.getGroupId())
                .eq(GroupEntity::getAppId, reqVO.getAppId())
                .isNull(GroupEntity::getDeletedAt)
                .set(GroupEntity::getGroupName, reqVO.getGroupName());
        groupMapper.update(null, updateWrapper);
        return true;
    }

    @Override
    public boolean delete(GroupDeleteReqVO reqVO) {
        GroupEntity groupEntity = new GroupEntity();
        groupEntity.setId(reqVO.getGroupId());
        groupEntity.setAppId(reqVO.getAppId());
        groupEntity.setStatus(AccessConstant.DEL_STATUS);
        groupEntity.setDeletedAt(LocalDateTime.now());
        groupEntity.setUpdatedAt(LocalDateTime.now());
        groupMapper.updateById(groupEntity);
        return true;
    }

    @Override
    public List<GroupVO> getAppList(String appId) {
        LambdaQueryWrapper<GroupEntity> where = Wrappers.lambdaQuery();
        where.eq(GroupEntity::getAppId, appId)
                .eq(GroupEntity::getStatus, AccessConstant.NORMAL_STATUS)
                .isNull(GroupEntity::getDeletedAt);
        List<GroupEntity> groupEntities = groupMapper.selectList(where);
        List<GroupVO> groupVos = new ArrayList<>();
        for (GroupEntity groupEntity : groupEntities) {
            GroupVO groupVO = new GroupVO();
            BeanUtils.copyProperties(groupEntity, groupVO);
            groupVos.add(groupVO);
        }
        return groupVos;
    }

    @Override
    public GroupVO getInfoByAppIdGroupIdStatus(String appId, Integer groupId, Integer status) {
        LambdaQueryWrapper<GroupEntity> where = Wrappers.lambdaQuery();
        where.eq(GroupEntity::getId, groupId)
                .eq(GroupEntity::getAppId, appId)
                .eq(GroupEntity::getStatus, status)
                .isNull(GroupEntity::getDeletedAt)
                .last("LIMIT 1");
        GroupEntity groupEntity = groupMapper.selectOne(where);
        GroupVO groupVO = new GroupVO();
        BeanUtils.copyProperties(groupEntity, groupVO);
        return groupVO;
    }

    @Override
    public List<GroupInfoRspVO> getListWithAccess(GroupInfoReqVO reqVO) {
        return groupAccessJoinMapper
                .getListWithAccess(reqVO.getAppId(), reqVO.getGroupId(), AccessConstant.NORMAL_STATUS);
    }

}
